﻿using Common;
using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using ZXIMEW.Models.CModels;
using ZXIMEW.Models.DModels;

namespace EWPlatAPP.Utility
{
    /// <summary>
    /// 辅助类 全局变量、方法
    /// </summary>
    public class CommonHelper
    {
        /// <summary>
        /// 实时变量参数集合
        /// </summary>
        public static Dictionary<string, ModbusParaSetInfo> CurrentParaAddress = new Dictionary<string, ModbusParaSetInfo>();

        /// <summary>
        /// 设置变量参数集合
        /// </summary>
        public static Dictionary<string, ModbusParaSetInfo> SetParaAddress = new Dictionary<string, ModbusParaSetInfo>();

        /// <summary>
        /// 报警参数集合
        /// </summary>
        public static List<AlarmParaInfo> AlarmParas = new List<AlarmParaInfo>();

        /// <summary>
        /// 报表参数集合
        /// </summary>
        public static List<ModbusParaSetInfo> ReportParas = new List<ModbusParaSetInfo>();

        /// <summary>
        /// 存储区列表
        /// </summary>
        public static List<StorageRegionInfo> StorageList = new List<StorageRegionInfo>();

        //串口对象
        public static SerialPort serialPort = null;

        //通信主站
        public static IModbusSerialMaster serialMaster = null;

        //通信状态
        public static bool OpenState = false;

        /// <summary>
        /// Modbus通信设置文件路径
        /// </summary>
        public static readonly string MSFileName = ConfigurationManager.AppSettings["msFileName"].ToString();

        /// <summary>
        /// 创建通信主站、打开连接
        /// </summary>
        /// <returns></returns>
        public static bool CreateSerialMaster()
        {
            ModbusSetInfo modbusSet = GetModbusSet();
            if (modbusSet.Connection == "串口")
            {
                serialPort = new SerialPort(modbusSet.Port, modbusSet.Baud, modbusSet.Parity, modbusSet.DataBits, modbusSet.StopBits);
                if (modbusSet.Mode == "RTU")
                {
                    serialMaster = ModbusSerialMaster.CreateRtu(serialPort);
                }
                else
                {
                    serialMaster = ModbusSerialMaster.CreateAscii(serialPort);
                }
            }
            serialPort.Open();
            return serialPort.IsOpen;
        }

        /// <summary>
        /// 获取Modbus设置
        /// </summary>
        /// <returns></returns>
        public static ModbusSetInfo GetModbusSet()
        {
            ModbusSetInfo modbusSet = new ModbusSetInfo();
            XmlDocument doc = new XmlDocument();
            if (File.Exists(MSFileName))
            {
                doc.Load(MSFileName);//加载xml文档
                XmlNode root = doc.SelectSingleNode("/ModbusSet");
                modbusSet.Connection = root.SelectSingleNode("Connection").InnerText;
                modbusSet.Mode = root.SelectSingleNode("Mode").InnerText;
                modbusSet.Port = root.SelectSingleNode("Port").InnerText;
                modbusSet.Baud = root.SelectSingleNode("Baud").InnerText.GetInt();
                modbusSet.DataBits = root.SelectSingleNode("DataBits").InnerText.GetInt();
                modbusSet.Parity = (Parity)Enum.Parse(typeof(Parity), root.SelectSingleNode("Parity").InnerText);
                modbusSet.StopBits = (StopBits)Enum.Parse(typeof(StopBits), root.SelectSingleNode("StopBits").InnerText);
            }
            return modbusSet;
        }
    }
}
